<?php

include("includes/theme-options.php");
include("includes/header_functions.php");
# Sidebar
if (function_exists('register_sidebar'))
{
	register_sidebar(array(
		'name'			=> 'Home/Page Left',
	    'before_widget'	=> '',
	    'after_widget'	=> '</div>',
	    'before_title'	=> '<h3>',
	    'after_title'	=> '</h3><div class="clear"></div><div class="box">',
	));		
	
    register_sidebar(array(
		'name'			=> 'Home Right #Full Width',
        'before_widget'	=> '',
        'after_widget'	=> '</div>',
        'before_title'	=> '<h3>',
        'after_title'	=> '</h3><div class="clear"></div><div class="box">',
    ));		
	
    register_sidebar(array(
		'name'			=> 'Home Right #Left',
        'before_widget'	=> '',
        'after_widget'	=> '</div>',
        'before_title'	=> '<h3>',
        'after_title'	=> '</h3><div class="clear"></div><div class="box">',
    ));	
	
    register_sidebar(array(
		'name'			=> 'Home Right #Right',
        'before_widget'	=> '',
        'after_widget'	=> '</div>',
        'before_title'	=> '<h3>',
        'after_title'	=> '</h3><div class="clear"></div><div class="box">',
    ));		
		
    register_sidebar(array(
		'name'			=> 'Page Right',
        'before_widget'	=> '',
        'after_widget'	=> '</div>',
        'before_title'	=> '<h3>',
        'after_title'	=> '</h3><div class="clear"></div><div class="box">',
    ));	
    
    register_sidebar(array(
    	'name'			=> 'Footer',
        'before_widget'	=> '',
        'after_widget'	=> '</div>',
        'before_title'	=> '<div class="footerwidget left"><h3>',
        'after_title'	=> '</h3>',
    ));	
}

# Limit Post
function tj_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content);

   if (strlen($_GET['p']) > 0) {
      echo "";
      echo $content;
      echo "...";
   }
   else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
        $content = substr($content, 0, $espacio);
        $content = $content;
        echo "";
        echo $content;
        echo "...";
   }
   else {
      echo "";
      echo $content;
   }
}

# Turn a category ID to a Name
function cat_id_to_name($id) {
	foreach((array)(get_categories()) as $category) {
    	if ($id == $category->cat_ID) { return $category->cat_name; break; }
	}
}

# Get image attachment (sizes: thumbnail, medium, full)
function get_thumbnail($postid=0, $size='full') {
	if ($postid<1) 
	$postid = get_the_ID();
	$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
	if ($thumb != null or $thumb != '') {
		echo $thumb; 
	}
	elseif ($images = get_children(array(
		'post_parent' => $postid,
		'post_type' => 'attachment',
		'numberposts' => '1',
		'post_mime_type' => 'image', )))
		foreach($images as $image) {
			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
			?>
<?php echo $thumbnail[0]; ?>
<?php
		}
	else {
		echo get_bloginfo ( 'stylesheet_directory' );
		echo '/images/image-pending.gif';
	}
	
}

# Automatically display/resize thumbnail
function tj_thumbnail($width, $height) {
?>

<a href="<?php the_permalink() ?>" rel="bookmark"> <img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&amp;h=<?php echo get_theme_mod($height); ?>&amp;w=<?php echo get_theme_mod($width); ?>&amp;zc=1" alt="<?php the_title(); ?>" /> </a>
<?php
}

# Breadcrumb
function the_breadcrumb() {
	if (!is_home()) {
		//echo 'You Are Here: ';
		echo '<a href="';
		echo get_option('home');
		echo '">';
		echo "Home";
		echo "</a> &raquo; ";
		if (is_category() || is_single()) {
			single_cat_title();
			if (is_single()) {
			the_category(', ');
				echo " &raquo; ";
				the_title();
			}
		} elseif (is_page()) {
			echo the_title();
		}
		  elseif (is_tag()) {
			echo 'Posts tagged with "'; 
			single_tag_title();
			echo '"'; }
		elseif (is_day()) {echo "Archive for "; the_time(' F jS, Y');}
		elseif (is_month()) {echo "Archive for "; the_time(' F, Y');}
		elseif (is_year()) {echo "Archive for "; the_time(' Y');}
		elseif (is_author()) {echo "Author Archive";}
		elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "Blog Archives";}
		elseif (is_search()) {echo "Search Results";}
	}
}


#Get recent comments
function tj_recent_comments($no_comments = 5, $comment_lenth = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 1, $hide_pingbacks_trackbacks = false) {
	global $wpdb;
	$most_recent_comments = wp_cache_get('most_recent_comments');
	
	if ( false === $most_recent_comments ) {
		$request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
		if( !$show_pass_post ) $request .= "AND post_password ='' ";
		if ( $hide_pingbacks_trackbacks ) $request .= "AND comment_type='' ";
		$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";

		$comments = $wpdb->get_results($request);
	
		$output = '';

		if ( $comments ) {
			$idx = 0;
			foreach ($comments as $comment) {
				$comment_author = stripslashes($comment->comment_author);
				if ($comment_author == "")
					$comment_author = "anonymous"; 
				$comment_content = strip_tags($comment->comment_content);
				$comment_content = stripslashes($comment_content);
				$words = split(" ", $comment_content); 
				$comment_excerpt = join(" ", array_slice($words, 0, $comment_lenth));
				$permalink = get_permalink($comment->ID) . "#comment-" . $comment->comment_ID;
				
				if ( 1 == $comment_style ) {
					$post_title = stripslashes($comment->post_title);
					$post_id= stripslashes($comment->post_id);
					$url = $comment->comment_author_url;
					$idx++;
					if ( 1 == $idx % 2 )
						$before = "<li>";
					else
						$before = "<li>";
					$output .= $before . "<a href='$permalink'>$comment_author</a>" . ' on <a href="' . get_permalink($comment->ID) . '">' . $post_title . '</a>' . $after;
				} else {
					$idx++;
					if ( 1 == $idx % 2 )
						$before = "<li class=''>";
					else
						$before = "<li class=''>";
		
					$output .= $before . '<strong>' . $comment_author . ':</strong> <a href="' . $permalink;
					$output .= '" title="View the entire comment by ' . $comment_author.'">' . $comment_excerpt.'</a>' . $after;
				}
			}

			$output = convert_smilies($output);
		} else {
			$output .= $before . "None found" . $after;
		}

		$most_recent_comments = $output;
		wp_cache_set('most_recent_comments', $most_recent_comments);
	}

	echo $most_recent_comments;
	}

# Get popular posts by comments count
function tj_popular_posts($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration='') {
global $wpdb;
$request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
$request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
if(!$show_pass_post) $request .= " AND post_password =''";
if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
}
$request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$comment_count = $post->comment_count;
$permalink = get_permalink($post->ID);
$output .= $before . '<a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '</a>' . $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
}

?>
